
#include "CanMgr.h"

#include <QMessageBox>
#include <QDebug>
//#include <QTimer>
#include <QTime>

CanMgr::CanMgr():QThread()
{
    m_Type = VCI_USBCAN2;
    m_Idx = 0;
    m_Chl = 0;

    m_IsOpen = false;
}


BOOL CanMgr::open(DWORD baudIdx)
{
    if(m_IsOpen == false)
    {
        DWORD ret = STATUS_OK;
        ret = VCI_OpenDevice(m_Type,m_Idx,0);//打开设备,只需一次
        if(ret != STATUS_ERR)
        {
            VCI_INIT_CONFIG initConfig;
            memset(&initConfig,0,sizeof(initConfig));
            initConfig.AccMask = 0xFFFFFFFF;
            initConfig.Mode = 0;
            initConfig.Timing0 = 0x00;  //1Mbps
            initConfig.Timing1 = 0x14;
            
            switch(baudIdx)
            {
               case 0:
                initConfig.Timing0 = 0x00;  //1Mbps
                initConfig.Timing1 = 0x14;
                break;
            case 1:
                initConfig.Timing0 = 0x00;  //800Kbps
                initConfig.Timing1 = 0x16;
                break;
            case 2:
                initConfig.Timing0 = 0x00;  //500Kbps
                initConfig.Timing1 = 0x1c;
                break;
            case 3:
                initConfig.Timing0 = 0x03;  //250Kbps
                initConfig.Timing1 = 0x1c;
                break;
            case 4:
                initConfig.Timing0 = 0x04;  //100Kbps
                initConfig.Timing1 = 0x1c;
                break;
            default:
                break;
            }
            
            ret = VCI_InitCAN(m_Type,m_Idx,m_Chl,&initConfig);//初始化设备
            if(ret != STATUS_ERR)
            {
                ret = VCI_StartCAN(m_Type,m_Idx,m_Chl);//开始采集
                if(ret != STATUS_ERR)
                {
                    qDebug()<<"Open CAN device success!"<<endl;
                    m_IsOpen = true;
                }
                else
                {
                    qDebug()<<"VCI_StartCAN ERR!"<<endl; 
                }
            }
            else
            {
                qDebug()<<"VCI_InitCAN ERR!"<<endl;
            }
        }
        else
        {
            qDebug()<<"VCI_OpenDevice ERR!"<<ret<<endl;
        }
    } 
    return m_IsOpen;
}

BOOL CanMgr::close(void)
{
    if(m_IsOpen != false)
    {
        DWORD ret = STATUS_OK;
        ret = VCI_CloseDevice(m_Type,m_Idx);
        if(ret != STATUS_ERR)
        {
            m_IsOpen = false;
            qDebug()<<"Close CAN device success!"<<endl;
        }
        else
        {
            qDebug()<<"Close CAN device fail!"<<endl;
        }
    }
    return !m_IsOpen;
}


/***********************************************/
// z 函数名称:直接发送操函数
// h 函数作用:NULL
// u 函数参数:NULL
// x 函数返回值:NULL
// y 备注:NULL
/***********************************************/
void CanMgr::send(VCI_CAN_OBJ info)
{
    DWORD Ret = VCI_Transmit(m_Type,m_Idx,m_Chl,&info,1);
    if(STATUS_OK == Ret)
    {
        qDebug()<<"send OK"<<endl;
#if 0
                QTime current_time =QTime::currentTime();
                int second = current_time.second(); //当前的秒
                int msec = current_time.msec();     //当前的毫秒
                qDebug()<<"time:"<< second << "."<< msec <<endl;
#endif
        emit sendedInfoSignal(&info);
    }
    else
    {
        qDebug()<<"send fail,ret:"<<Ret<<endl;
    }
}


/**
 *函数名:线程
 *函数参数:NULL
 *函数作用:NULL
 *函数返回值:NULL
 *备注:NULL
 */
void CanMgr::run()
{
    VCI_ClearBuffer(m_Type,m_Idx,m_Chl);

    while(m_IsOpen)
    {
        if(VCI_GetReceiveNum(m_Type,m_Idx,m_Chl) > 0)
        {
            quint32 recvCount = VCI_Receive(m_Type,m_Idx,m_Chl,recvObj,10);
            if(0xFFFFFFFF == recvCount)//读取失败
            {
                qDebug()<<"VCI_Receive err!"<<endl;
            }
            else
            {
                //发送给数据线程处理
                qDebug()<<"received msg!"<<endl;
#if 0
                QTime current_time =QTime::currentTime();
                int second = current_time.second(); //当前的秒
                int msec = current_time.msec();     //当前的毫秒
                qDebug()<<"time:"<< second << "."<< msec <<endl;
#endif
                emit getCanData(recvObj,recvCount);
            }
        }
        else
        {
            Sleep(1);
        }
    }
}

